using System.Data;
using System.Data.SqlClient;
using Provausio.Common.Data;
using Provausio.Common.Objects;
using Provausio.Common.Utility.Extensions;

namespace TrainingManagement.api.Models.Data.CRUD.Skill
{
    public class SkillInsertDataSource : NonQuerySource<BusinessObjects.Skill>
    {
        public override bool Execute(BusinessObjects.Skill request)
        {
            const string skillInsert = "dbo.training_api_SkillInsert";
            var parameters = new SqlParameter[3];
            parameters[0] = new SqlParameter("@Title", SqlDbType.NVarChar){Value = request.Title};
            parameters[1] = new SqlParameter("@DocID", SqlDbType.Int){Value = request.DocId};
            parameters[2] = new SqlParameter("@SkillID", SqlDbType.Int){Direction = ParameterDirection.Output};

                var result = SqlHelper.ExecuteNonQuery(
                        ConnectionStrings.TrainingDb,
                        CommandType.StoredProcedure,
                        skillInsert,
                        parameters) != 0;
                request.SkillId = parameters[2].Value.DbTo<int>();
                return result;
        }
    }
}